var requestSign = require('../../../../utils/requestData.js');
var api = require('../../../../utils/api.js').open_api;
var util = require('../../../../utils/util.js');
var header = getApp().header;
var re = require('../../../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    store_list:'',
    page_index:1,
    page_size:20,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserLocation();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 获取平台下所有门店列表
  getAllStoreListForWap:function(){
    const that = this;
    let postData = {
      'page_index': that.data.page_index,
      'page_size': that.data.page_size,
      'lng': that.data.lng,
      'lat': that.data.lat,
    }
    let datainfo = requestSign.requestSign(postData);
    header.sign = datainfo;
    re.request(api.get_getAllStoreListForWap, postData, header).then((res) => {      
      if(res.data.code == 1){
        that.setData({
          store_list: res.data.data.store_list
        })
      }else{
        wx.showModal({
          title: '提示',
          content: res.data.message,
        })
      }
    })
  },

  //获取当前位置的经纬度
  getUserLocation: function () {
    const that = this;    
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        //纬度，范围为 -90~90，负数表示南纬
        const latitude = res.latitude
        //经度，范围为 -180~180，负数表示西经
        const longitude = res.longitude
        that.setData({
          lng: longitude,
          lat: latitude
        })
        that.getAllStoreListForWap();
      },
    })
  },

  openLocation:function(e){
    const that = this;
    let latitude = parseFloat(e.currentTarget.dataset.lat);
    let longitude = parseFloat(e.currentTarget.dataset.lng);
    wx.openLocation({
      latitude,
      longitude,
      scale: 18
    })
  },

  //跳转到店铺首页
  onshopHomePage:function(e){
    const that = this;
    let shop_id = e.currentTarget.dataset.shopid;
    let onPageData = {
      url: '/pages/shop/home/index',
      num: 4,
      param: '?shopId=' + shop_id,
    }
    util.jumpPage(onPageData);
  },
})